package org.kjk.board;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BoardController {
	@Resource(name = "boardDao")
	public BoardDAOImpl dao;

	@RequestMapping(value = "/writeBoardRedirect.do", method = RequestMethod.GET)
	protected String writeRedirect() throws Exception {
		return "board/write.jsp";
	}

	@RequestMapping(value = "/insertBoard.do", method = RequestMethod.POST)
	public ModelAndView insertBoard(@RequestParam("subject") String subject,
			@RequestParam("content") String content,
			@RequestParam("writer") String writer, HttpServletResponse response)
			throws IOException {
		System.out.println("call insert board " + " subject : " + subject
				+ " content : " + content);
		BoardDTO bean = new BoardDTO(subject, content, writer);
		dao.insert(bean);
		response.getWriter().println("success");
		return new ModelAndView("../searchBoard.do");
	}

	@RequestMapping(value = "/searchBoard.do", method = RequestMethod.GET)
	public ModelAndView searchBoard(HttpServletRequest request) {
		System.out.println("call board service");
		String queryType = request.getParameter("type");
		String queryField = request.getParameter("field");
		String sql = null;
		List<BoardDTO> result = null;
		String[] param = null;
		if (queryType == null) {
			sql = "select * from board order by serial desc";
		} else if (queryType.trim().equals("1")) {
			sql = "select * from board where subject like '%?%' order by serial desc";
			param = new String[] { queryField };
		} else if (queryType.trim().equals("2")) {
			sql = "select * from board where content like '%?%' order by serial desc";
			param = new String[] { queryField };
		} else {
			sql = "select * from board order by serial desc";
		}

		result = (List<BoardDTO>) dao.select(sql, param);
		return new ModelAndView("/WEB-INF/board/board.jsp", "board", result);
	}

	@RequestMapping(value = "/searchJsonBoard.do")
	public void searchJsonBoard(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String queryType = request.getParameter("type");
		String queryField = request.getParameter("field");

		String sql = null;
		String[] param = null;
		if (queryType == null) {
			sql = "select * from board";
		} else if (queryType.trim().equals("1")) {
			sql = "select * from board where subject like '%?%'";
			param = new String[] { queryField };
		} else if (queryType.trim().equals("2")) {
			sql = "select * from board where content like '%?%'";
			param = new String[] { queryField };
		} else {
			sql = "select * from board";
		}

		JSONObject json = dao.selectJson(sql, param);
		PrintWriter out = response.getWriter();
		out.println(json);
	}

	@RequestMapping(value = "/searchBoardContent.do", method = RequestMethod.GET)
	public ModelAndView searchBoardContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("call board service");
		String queryIndex = request.getParameter("index");

		String sql = null;
		String[] param = null;
		if (queryIndex == null) {
			return new ModelAndView("/WEB-INF/searchBoard.do");
		} else {
			sql = "select * from board where serial=?";
			param = new String[] { queryIndex.trim() };
		}
		BoardDTO dto = (BoardDTO) dao.select(sql, param).get(0);
		dto.setContent(dto.getContent().replace("<br/>", "\n"));
		return new ModelAndView("/WEB-INF/board/readboard.jsp", "board", dto);
	}
}
